package com.kingdee.hackson.controller;

import com.kingdee.hackson.service.IAuthService;
import com.kingdee.hackson.utils.AdminStructNumberUtils;
import com.kingdee.hackson.utils.IDConvertUtils;
import com.kingdee.hackson.utils.OrgCodeConvertUtils;
import com.kingdee.hackson.entity.AuthEntity;
import com.kingdee.hackson.entity.BizDataEntity;
import com.kingdee.hackson.service.IBizDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试自动生成代码的逻辑Controller
 *
 * @author songwh
 * @date 2021-12-09
 */
@RestController
@RequestMapping("/init")
public class InitDataController {
    private Logger logger = LoggerFactory.getLogger(InitDataController.class);

    @Autowired
    private IBizDataService bizDataService;

    @Autowired
    private IAuthService authService;

    @GetMapping("/data/{num}")
    public String batchInitBizData(@PathVariable int num) {
        Long count = authService.getIdListCountByParentPrefixCode("0");
        // 清空表
        bizDataService.truncate();
        // 构建数据
        List<BizDataEntity> datas = new ArrayList<>();
        for (long i = 1; i <= num; i++) {
            Long id = IDConvertUtils.encodeToLongId(i);
            BizDataEntity taskAddTest = new BizDataEntity();
            taskAddTest.setId(id);
            taskAddTest.setName("name" + i);
            taskAddTest.setNumber("number" + i);
            taskAddTest.setEnabled("1");
            taskAddTest.setStatus("A");
            taskAddTest.setCreator(1L);
            taskAddTest.setModifier(1L);
            long v = (long) (Math.random() * count + i);
            // long authId = (v) % count;
            long authId = (long) (Math.random() * count);
            String authCode = OrgCodeConvertUtils.formatTo26Code((int) authId);
            taskAddTest.setOrgId(IDConvertUtils.encodeToLongId(authId));
            taskAddTest.setLevelCode(authCode);
            taskAddTest.setAdminStruct(AdminStructNumberUtils.encodeStructNumber(authCode));
            datas.add(taskAddTest);
            if (i % 1000 == 0) {
                logger.info("add biz data i = {}", i);
                bizDataService.batchInsert(datas);
                datas = new ArrayList<>();
            }
        }
        if (!CollectionUtils.isEmpty(datas)) {
            bizDataService.batchInsert(datas);
        }
        return "success";
    }

    @GetMapping("/auth/{num}")
    public String batchInitAuthData(@PathVariable int num) {
        authService.truncate();
        authService.addRoot();
        List<AuthEntity> datas = new ArrayList<>();
        for (int i = 1; i < num; i++) {
            Long id = IDConvertUtils.encodeToLongId(i);
            AuthEntity authTest = new AuthEntity();
            authTest.setId(id);
            authTest.setName("name" + i);
            authTest.setNumber("number" + i);
            authTest.setLevelCode(OrgCodeConvertUtils.formatTo26Code(i));
            authTest.setParentId(IDConvertUtils.encodeToLongId(OrgCodeConvertUtils.getParentIdBy26Code(authTest.getLevelCode())));
            authTest.setAdminStruct(AdminStructNumberUtils.encodeStructNumber(authTest.getLevelCode()));
            datas.add(authTest);
            if (i % 1000 == 0) {
                logger.info("add auth data i = {}", i);
                authService.batchInsert(datas);
                datas.clear();
            }
        }
        if (!CollectionUtils.isEmpty(datas)) {
            JdbcTemplate jdbcTemplate = new JdbcTemplate();
            authService.batchInsert(datas);
        }
        return "success";
    }
}
